package com.hmdp.config;

import com.hmdp.interceptor.LoginInterceptor;
import com.hmdp.interceptor.RefreshTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * date: 2023/9/7 22:29
 * author: MR.孙
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private static List<String> whiteList = new CopyOnWriteArrayList<>();


    static {
        whiteList.add("/shop/**");
        whiteList.add("/shop-type/**");
        whiteList.add("/upload/**");
        whiteList.add("/voucher/**");
        whiteList.add("/blog/hot");
        whiteList.add("/user/login");
        whiteList.add("/user/code");

    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器 loginInterceptor没有被Spring管理
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(whiteList)//指定放行规则
                .order(1)
        ;

        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .order(0)
        ;



    }
}
